package com.fruit.util.sms.code;

import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import com.fruit.pojo.sms.SmsTemplet;
import com.fruit.util.CustomUtil;
import com.fruit.util.HttpClientManagerT;
import com.fruit.util.sms.NeteaseObj;
import com.fruit.util.sms.checksum.CheckSumBuilder;

/**
 * 网易云信
 * @author Administrator
 *
 */
public class NeteaseSms {
	

	
	public static String newPost(SmsTemplet st, List<NameValuePair> nvps){
		String result = null;
		HttpPost httpPost = new HttpPost(st.getUrl());
		String nonce = String.valueOf(CustomUtil.buildRandom(6));
		String curTime = String.valueOf((new Date()).getTime() / 1000L);//time
		httpPost.addHeader("AppKey", st.getAppKey());
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        String checkSum = CheckSumBuilder.getCheckSum(st.getAppSecret(), nonce ,curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        try{
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        result = HttpClientManagerT.postData(httpPost);
        }catch(Exception e){
        	result = "exception";
        }
        return result;
        
	}
	
	

	public static void batchSms(final NeteaseObj sms){
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < sms.getMobile().length; i++) {
					//sendTemplateSms(sms.getTemplateId(), sms.getMobile()[i], (sms.getParams()[i]).toString());
				}
			}
		}).start();
	}

	
}
